import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CustomRadioGroup<T> extends StatelessWidget {
  final List<T> values;
  final List<String> labels;
  final T groupValue;
  final ValueChanged<T> onChanged;
  final double? height;
  final double? width;
  final double spacing;
  final EdgeInsets? padding;
  final double borderRadius;

  const CustomRadioGroup({
    Key? key,
    required this.values,
    required this.labels,
    required this.groupValue,
    required this.onChanged,
    this.height = 36,
    this.width,
    this.spacing = 4,
    this.padding,
    this.borderRadius = 4,
  })  : assert(values.length == labels.length),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: GridView.builder(
        physics: const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: values.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          mainAxisSpacing: 10, // 行间距
          crossAxisSpacing: 16,
          childAspectRatio: 98 / 36,
        ),
        itemBuilder: (context, index) =>
            _buildRadioItem(values[index], labels[index]),
      ),
    );
  }

  Widget _buildRadioItem(T value, String label) {
    final isSelected = value == groupValue;
    return GestureDetector(
      onTap: () => onChanged(value),
      child: Container(
        height: height,
        padding: padding ?? const EdgeInsets.symmetric(horizontal: 10),
        decoration: BoxDecoration(
          color: isSelected ? Color(0xFFFFF3BD) : Colors.white,
          border: Border.all(
            color: !isSelected ? Color(0xFFF1F1F1) : Colors.transparent,
            width: 1,
          ),
          borderRadius: BorderRadius.circular(borderRadius),
        ),
        child: Center(
          child: Text(
            label,
            style: TextStyle(
              color: Color(0xFF333333),
              fontSize: 14.sp,
            ),
          ),
        ),
      ),
    );
  }
}
